package com.base.andlang2

import android.content.Intent
import android.view.KeyEvent
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.base.andlang2.databinding.ActivityMainBinding
import com.base.andlangcore.mvvm.AndLangActivity
import com.base.andlangcore.mvvm.AndLangModel
import com.base.andlangui.view.bottomtab.SpecialTab
import com.base.andlangui.view.bottomtab.SpecialTabRound
import com.base.andlangrouter.RouteUrl
import me.majiajie.pagerbottomtabstrip.NavigationController
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener
import kotlin.system.exitProcess

@Route(path = RouteUrl.APP_MAIN_ACTIVITY)
class MainActivity : AndLangActivity<ActivityMainBinding,AndLangModel>() {
    @JvmField
    @Autowired
    var position: Int = 0

    private val fragments = ArrayList<Fragment>()
    private var navigationController: NavigationController? = null

    override fun getViewBinding(): ActivityMainBinding =ActivityMainBinding.inflate(layoutInflater)

    override fun getVM(): AndLangModel = ViewModelProvider(this)[AndLangModel::class.java]

    override fun initView() {
        ARouter.getInstance().inject(this)
        initTab()
    }

    override fun initData() {

    }

    private fun initTab(){
        val oneFragment =
            ARouter.getInstance().build(RouteUrl.APP_HOME_ONE).navigation() as Fragment
        fragments.add(oneFragment)

        val twoFragment =
            ARouter.getInstance().build(RouteUrl.APP_HOME_TWO).navigation() as Fragment
        fragments.add(twoFragment)

        val threeFragment =
            ARouter.getInstance().build(RouteUrl.APP_HOME_THREE).navigation() as Fragment
        fragments.add(threeFragment)

        supportFragmentManager
            .beginTransaction()
            .replace(R.id.fl_container, fragments[0])
            .commitNow()

        navigationController = vb.bottomNav.custom()
            .addItem(
                SpecialTab.newItem(
                    this,
                    R.drawable.ic_home_black_24dp,
                    R.drawable.ic_home_black_24dp,
                    "首页"
                )
            )
            .addItem(
                SpecialTabRound.newRoundItem(
                    this,
                    R.drawable.ic_dashboard_black_24dp,
                    R.drawable.ic_dashboard_black_24dp,
                    "分享"
                )
            )
            .addItem(
                SpecialTab.newItem(
                    this,
                    R.drawable.ic_notifications_black_24dp,
                    R.drawable.ic_notifications_black_24dp,
                    "通知"
                )
            ).build()

        navigationController?.addTabItemSelectedListener(object :
            OnTabItemSelectedListener {

            override fun onSelected(index: Int, old: Int) {
                switchPage(index, old)
            }

            override fun onRepeat(index: Int) {

            }
        })
        navigationController?.setSelect(position, true)

    }
    private fun switchPage(index: Int, old: Int) {
        val now = fragments[index]
        supportFragmentManager.beginTransaction().apply {
            if (!now.isAdded) {
                add(R.id.fl_container, now)
            }
            show(now)
            hide(fragments[old])
            commit()
        }
    }


    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        position = intent?.getIntExtra("position", 0)!!
        navigationController?.setSelect(position, true)
    }

    //按两下返回键退出
    private var mExitTime: Long = 0
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - mExitTime > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show()
                mExitTime = System.currentTimeMillis()
            } else {
                exitProcess(0)
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}